package com.example.itemdecorationdemo.fragment


import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.itemdecorationdemo.R
import com.example.itemdecorationdemo.model.MainViewModel
import org.w3c.dom.Text

class MainFragment : Fragment() {
    private var titleView: TextView? = null
    private var mViewModel: MainViewModel? = null
    private var recyclerView: RecyclerView? = null
    private val list = mutableListOf<MainBean>()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.main_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        initData()
    }

    private fun initData() {

        list.add(MainBean("截图DrawingCache") {
            Log.d("zhangyu", "分割线")
            testShotDrawingCache()
        })
        list.add(MainBean("截图ViewDraw") {
            Log.d("zhangyu", "分割线")
            testShotViewDraw()
        })

        list.add(MainBean("默认") {
            Log.d("zhangyu", "分割线")
            val bundle = Bundle()
            bundle.putInt("type", 0)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("分割线1") {
            Log.d("zhangyu", "分割线")
            val bundle = Bundle()
            bundle.putInt("type", 1)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("分割线2") {
            Log.d("zhangyu", "分割线")
            val bundle = Bundle()
            bundle.putInt("type", 2)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("分割线3") {

            val bundle = Bundle()
            bundle.putInt("type", 3)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("tag") {

            val bundle = Bundle()
            bundle.putInt("type", 4)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("tag+分割线") {
            val bundle = Bundle()
            bundle.putInt("type", 5)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })


        list.add(MainBean("Section") {
            Log.d("zhangyu", "Section")
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SectionFragment.newInstance())
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("粘性部位-头部") {
            val bundle = Bundle()
            bundle.putInt("type", 6)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("粘性头部") {
            val bundle = Bundle()
            bundle.putInt("type", 7)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })





        list.add(MainBean("粘性头部-测试") {
            val bundle = Bundle()
            bundle.putInt("type", 8)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("ItemDecoration-测试") {
            val bundle = Bundle()
            bundle.putInt("type", 9)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, SimpleDividerFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("Grid RecyclerView VERTICAL") {
            val bundle = Bundle()
            bundle.putInt("type", 100)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, GridTestFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })
        list.add(MainBean("Grid RecyclerView HORIZONTAL") {
            val bundle = Bundle()
            bundle.putInt("type", 101)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, GridTestFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("Grid RecyclerView All - 10") {
            val bundle = Bundle()
            bundle.putInt("type", 102)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, GridTestFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

        list.add(MainBean("Grid RecyclerView 均等分割线") {
            val bundle = Bundle()
            bundle.putInt("type", 103)
            parentFragmentManager.beginTransaction()
                .add(R.id.container, GridTestFragment::class.java, bundle)
                .addToBackStack(null)
                .commit()
        })

    }

    @SuppressLint("WrongCall")
    private fun testShotViewDraw() {
        titleView?.let {
            val bitmap = Bitmap.createBitmap(
                it.width, it.height, Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(bitmap)
            it.draw(canvas)
            canvas.setBitmap(null)
        }
    }

    private fun testShotDrawingCache() {
        recyclerView?.isDrawingCacheEnabled = true
        val bitmap = recyclerView?.drawingCache

        recyclerView?.isDrawingCacheEnabled = false
        recyclerView?.destroyDrawingCache()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerView = view.findViewById(R.id.main_fragment_rv)
        titleView = view.findViewById(R.id.test_title)
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView?.setLayoutManager(layoutManager)
        recyclerView?.adapter = object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.main_fragment_item, parent, false)
                return ViewHolder(view)
            }

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                //holder.title.text = "第" + position + "个item"
                holder.title.text = list[position].name
                holder.itemView.setOnClickListener {
                    list[position].click.invoke()
                }
            }

            override fun getItemCount(): Int {
                return list.size
            }
        }
    }


    data class MainBean(val name: String, val click: () -> Unit)

    private class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: TextView = itemView.findViewById(R.id.main_fragment_item_title)
    }

    companion object {
        @JvmStatic
        fun newInstance(): MainFragment {
            return MainFragment()
        }
    }
}